package me.zhengjie.modules.web.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.OrderPayRequest;
import me.zhengjie.modules.web.domain.ReturnPayInfoVO;
import me.zhengjie.modules.web.domain.order.OrderCancelRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.domain.pay.PayGatewayRequest;
import me.zhengjie.modules.web.service.pay.OrderCancelFactory;
import me.zhengjie.modules.web.service.pay.OrderSubmitFactory;
import me.zhengjie.modules.web.service.pay.PayGatewayFactory;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("web/order")
@Api(tags = "前端接口-订单")
public class OrderController {


    @PostMapping("payGateway")
    @ApiOperation("WEB-统一支付网关")
    public ResponseBaseEntity payGateway(@RequestBody OrderPayRequest requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isAnyBlank(requestEntity.getOrderSn()) || Objects.isNull(requestEntity.getOrderType())){
                throw new WeChatException("orderSn orderType exist null");
            }
            PayGatewayRequest request = new PayGatewayRequest();
            request.setOrderSn(requestEntity.getOrderSn());
            request.setOpenId(ATMRContext.getContext().getOpenId());
            ReturnPayInfoVO execute = PayGatewayFactory.orderType(requestEntity.getOrderType()).execute(request);
            response.setBody(execute);
        }catch (WeChatException e){
            response.getHeader().setErrCode(StringUtils.isNotBlank(e.getCodeStr()) ? e.getCodeStr() : String.valueOf(e.getCode()));
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("submit")
    @ApiOperation("WEB-统一创单接口")
    public ResponseBaseEntity submit(@RequestBody OrderSubmitRequest requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            OrderSubmitResponse execute = OrderSubmitFactory.orderType(requestEntity.getOrderType()).execute(requestEntity);
            response.setBody(execute);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("cancel")
    @ApiOperation("WEB-统一取消订单接口")
    public ResponseBaseEntity cancel(@RequestBody OrderCancelRequest requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (Objects.isNull(requestEntity.getOrderType()) || StringUtils.isBlank(requestEntity.getOrderSn())){
                throw new WeChatException("缺少参数");
            }
            boolean execute = OrderCancelFactory.orderType(requestEntity.getOrderType()).execute(requestEntity);
            response.setBody(execute);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
